package com.wejoy.video.middle.station.data.service.screen.impl;

import cn.hutool.core.util.ObjectUtil;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.screen.IScreenUserCarmeraConfigMapper;
import com.wejoy.video.middle.station.data.domain.screen.ScreenUserCarmeraConfig;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.screen.IScreenUserCarmeraConfigService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户大屏监控记忆配置 
 *
 * @author miracle_j
 * @date 2024年11月30日 14:15:42
 */
@Service
public class ScreenUserCarmeraConfigServiceImpl implements IScreenUserCarmeraConfigService {
    @Autowired
    private IScreenUserCarmeraConfigMapper screenUserCarmeraConfigMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(ScreenUserCarmeraConfig req, String operator) {
        ScreenUserCarmeraConfig one = getOneByUserId(req.getUserId(), req.getType());
        if(!ObjectUtil.isEmpty(one)){
            req.setId(one.getId());
        }
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(ScreenUserCarmeraConfig req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return screenUserCarmeraConfigMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(ScreenUserCarmeraConfig screenUserCarmeraConfig) {
        WJAssert.notNull(screenUserCarmeraConfig, "入参对象不能为空");
        WJAssert.notNull(screenUserCarmeraConfig.getId(), "id参数错误");
        ScreenUserCarmeraConfig where = new ScreenUserCarmeraConfig();
        where.setId(screenUserCarmeraConfig.getId());
        return screenUserCarmeraConfigMapper.updateSelective(screenUserCarmeraConfig, where);
    }

    @Override
    public int getCount(ScreenUserCarmeraConfig screenUserCarmeraConfig) {
        checkStringLength(screenUserCarmeraConfig, true);
        return screenUserCarmeraConfigMapper.getCount(screenUserCarmeraConfig);
    }

    @Override
    public ScreenUserCarmeraConfig getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        ScreenUserCarmeraConfig where = new ScreenUserCarmeraConfig();
        where.setId(id);
        return screenUserCarmeraConfigMapper.getOne(where);
    }

    @Override
    public ScreenUserCarmeraConfig getOneByUserId(Long id,Integer type) {
        WJAssert.notNull(id, "id参数错误");
        ScreenUserCarmeraConfig where = new ScreenUserCarmeraConfig();
        where.setUserId(id);
        where.setType(type);
        return screenUserCarmeraConfigMapper.getOne(where);
    }

    @Override
    public List<ScreenUserCarmeraConfig> getList(ScreenUserCarmeraConfig screenUserCarmeraConfig) {
        checkStringLength(screenUserCarmeraConfig, true);
        return screenUserCarmeraConfigMapper.getList(screenUserCarmeraConfig);
    }

    @Override
    public List<ScreenUserCarmeraConfig> getPageList(ScreenUserCarmeraConfig screenUserCarmeraConfig) {
        checkStringLength(screenUserCarmeraConfig, true);
        return screenUserCarmeraConfigMapper.getPageList(screenUserCarmeraConfig);
    }

    @Override
    public void audit(Long id, String operator) {
        ScreenUserCarmeraConfig req = new ScreenUserCarmeraConfig();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        ScreenUserCarmeraConfig req = new ScreenUserCarmeraConfig();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        ScreenUserCarmeraConfig req = new ScreenUserCarmeraConfig();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(ScreenUserCarmeraConfig screenUserCarmeraConfig, boolean nullAble) {
        WJAssert.notNull(screenUserCarmeraConfig, "入参对象不能为空");
        WJAssert.limitMaxLength(screenUserCarmeraConfig.getPointId(), true, 65535, "点位ids不合法!");
        WJAssert.limitMaxLength(screenUserCarmeraConfig.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(screenUserCarmeraConfig.getRemark(), true, 255, "备注不合法!");
    }
}
